/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ufcg.dsc.lsi.vademecum.context.provider;

import br.edu.ufcg.dsc.lsi.vademecum.context.ContextIF;
import br.edu.ufcg.dsc.lsi.vademecum.context.LocationContext;
import br.edu.ufcg.dsc.lsi.vademecum.context.RawData;
import br.edu.ufcg.dsc.lsi.vademecum.util.URLUtils;
import gov.nist.siplite.ConfigurationProperties;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.location.Coordinates;
import javax.microedition.location.Location;

/**
 * Class that provides the location Context from sensed data of GPS.
 * @author Hugo Feitosa de Figueirêdo.
 */
public class LocationContextProvider implements ContextProviderIF, RawDataListener {
    public static final String VADEMECUM_UPDATE_SERVICE_URL = "http://localhost:8400/VadeMecum2/sparql_update?";

    public void updateContext(ContextIF context) {
        System.out.println("Atualizou o contexto localizacional");
        Hashtable data = context.getContextData();
        data.keys();
        String xValue = (String) data.get("x");
        String yValue = (String) data.get("y");
        String contextClass = context.getContextClass();
        String url = VADEMECUM_UPDATE_SERVICE_URL;
        //url += "request=";
        String request = "" +
                "PREFIX vade: <http://www.lsi.dsc.ufcg.edu.br/vademecum.owl#> \n" +
                "PREFIX dc:      <http://purl.org/dc/elements/1.1/> \n" +
                "PREFIX rdfs:   <http://www.w3.org/2000/01/rdf-schema#> \n" +
                "PREFIX rdf:      <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                "MODIFY\n" +
                "DELETE {" +
                    "?c vade:x ?x ." +
                    "?c vade:y ?y" +
                "}\n" +
                "INSERT {" +
                    "?c vade:x " + xValue + ". " +
                    "?c vade:y " + yValue + ". " +
                "}\n" +
                "WHERE {" +
                    "vade:Hugo vade:hasContext ?c .\n" +
                    "?c rdf:type " + contextClass + " .\n" +
                    "?c vade:x ?x . \n" +
                    "?c vade:y ?y " +
                "}";
        //url += URLUtils.urlEncode(request);
        //url += request;
        System.out.println(url);
        //url = "http://buchada.dsc.ufcg.edu.br/vademecum2/sparql?query=PREFIX+vade:+<http://www.lsi.dsc.ufcg.edu.br/vademecum.owl%23>%0D%0APREFIX+dc:++++++<http://purl.org/dc/elements/1.1/>%0D%0APREFIX+rdfs:%09+++<http://www.w3.org/2000/01/rdf-schema%23>%0D%0APREFIX+rdf:++++++<http://www.w3.org/1999/02/22-rdf-syntax-ns%23>%0D%0ASELECT+%3Fb%0D%0AWHERE+%0D%0A++{+%0D%0A++++vade:Hugo+vade:hasContext+%3Fc+.%0D%0A++++%3Fc+rdf:type+vade:Emotion+.%0D%0A++++%3Fc+vade:emotionValue+%3Fb%0D%0A++}&stylesheet=xml-to-html.xsl";
        HttpConnection hc = null;

        OutputStream out = null;

        try {
            hc = (HttpConnection) Connector.open(url);
            //hc.setRequestProperty("User-agent", "Mozilla/4.0");
            hc.setRequestProperty("User-Agent",
            "Profile/MIDP-1.0 Configuration/CLDC-1.0");
            hc.setRequestMethod(HttpConnection.POST);

            int ch;

            out = hc.openOutputStream();
            out.write(request.getBytes());
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }

                if (hc != null) {
                    hc.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    public ContextIF rawDataProcessor(RawData rawData) {
        Location location = (Location) rawData.getSensedData();
        Coordinates coordinates = location.getQualifiedCoordinates();
        double latitude = coordinates.getLatitude();
        double longitude = coordinates.getLongitude();
        ConfigurationProperties properties = new ConfigurationProperties();
        properties.setProperty("y", Double.toString(latitude));
        properties.setProperty("x", Double.toString(longitude));
        LocationContext locationContext = new LocationContext();
        locationContext.setContextData(properties);
        return locationContext;
    }

    public void sensedDataUpdated(RawDataProviderIF dataProvider, RawData rawData) {
        ContextIF context = rawDataProcessor(rawData);
        updateContext(context);
    }
}
